一個專業的 倉儲歷史 是一段精心編排的敘事,而非隨意的日記。透過優先考慮 原子快照 與策略性重基底(rebase),開發者能將時間軸轉化為可搜尋、易讀的文件。
1. 提交作為有意圖的快照
不要將 git commit 當作「儲存」按鈕,而應將每次提交視為一個邏輯里程碑。簡單原則:「為專案中每一項重要的新增內容提交一次快照」,以及「若無法提出明確且具體的訊息,就不要提交。」
2. 線性理想的實踐
重基底(Rebasing) 透過將分支移動至另一分支的末端,實現快速前進式合併(fast-forward merge)。這有效消除了合併提交的需求,從而產生完全 線性歷史。
3. 紀律與精煉
像 git commit --amend 讓你能夠將暫存的變更加入到最近一次提交中。這能確保敘事完整性,於推送到共用遠端之前修正錯誤。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which command is used to add staged changes to the most recent commit instead of creating a new one?
git merge --no-ff
git commit --amend
git rebase --continue
git reset --soft
✅ Correct!
Correct! --amend lets you refine your latest local commit.❌ Incorrect
Check the quick reference for modifying the most recent snapshot.QUESTION 2
What is the primary benefit of rebasing according to the 'Linear Ideal'?
It preserves every single 'work-in-progress' commit.
It effectively eliminates the need for merge commits.
It prevents the use of git log.
It automatically pushes changes to the remote.
✅ Correct!
Yes! Rebasing allows for fast-forward merges, keeping the history vertical and readable.❌ Incorrect
Rebasing focuses on moving a branch to the tip of another to avoid 'tangled' merge bubbles.QUESTION 3
True or False: You should commit a snapshot even if you cannot describe it with a single specific message.
True
False
✅ Correct!
False. If you can't name the change specifically, it likely isn't an atomic or significant snapshot.❌ Incorrect
The philosophy suggests waiting until you have a specific, logical addition to commit.QUESTION 4
What does
git merge --no-ff accomplish?It deletes the branch after merging.
It forces a merge commit even if a fast-forward is possible.
It performs a rebase instead of a merge.
It ignores all local changes.
✅ Correct!
Correct. This is used to intentionally create a visible milestone in the repository history.❌ Incorrect
The --no-ff flag prevents Git from simply moving the pointer forward without a merge node.QUESTION 5
Which command provides a condensed, one-line view of the commit history?
git status --short
git log --oneline
git show --summary
git reflog
✅ Correct!
Yes! This is the standard tool for auditing a clean, linear history.❌ Incorrect
Try the log command variation that keeps things to 'one line' per entry.Case Study: The Feature Cleanup
Applying the Narrative Philosophy
A developer has five local commits: 'fix typo', 'tmp', 'wip', 'actual fix', and 'forgot one file'. They want to merge this into the master branch as a single professional entry labeled 'Implement user authentication'.
Q
1. Which tool should the developer use to combine these five messy commits into one before merging?
Solution:
The developer should use an interactive rebase (
The developer should use an interactive rebase (
git rebase -i) to 'squash' the work-in-progress commits into a single, clean snapshot.Q
2. If the developer wants to ensure the 'master' branch remains linear without 'bubbles' from this feature, what merge strategy should they use?
Solution:
They should use a fast-forward merge (default) or rebase the feature branch onto the tip of master first. This moves the branch pointer to the tip without a merge commit.
They should use a fast-forward merge (default) or rebase the feature branch onto the tip of master first. This moves the branch pointer to the tip without a merge commit.